package net.libra.ui.tip
{
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	
	import net.libra.ui.layer.LayerManager;
	
	import com.cw.ui.TipBgUI;
	
	
	public class ToolTip extends BaseTip
	{
		private var _display:TipBgUI;
		public function ToolTip()
		{
			super();
			_display = new TipBgUI();
			
			_display.content.x = 9;
			_display.content.y = 9;
			_display.content.textColor = 0xDCD9B9;
			_display.content.autoSize = "left";
			_display.content.multiline = true;
			_display.content.wordWrap = false;
			
			var tf:TextFormat = _display.content.defaultTextFormat;
			tf.color = 0xDCD9B9;
			_display.content.defaultTextFormat = tf;
			
			content = "";
		}
		override public function get display():DisplayObject
		{
			return _display;
		}
		
		
		
		public function set content(value:String):void
		{
			value = value||"";
			_display.content.wordWrap = false;
			_display.content.htmlText = ""+value+"";
			if(_display.content.width > 244)
			{
				_display.content.wordWrap = true;
				_display.content.width = 244;
			}
			_display.background.width = contentWidth + 18;
//			trace("backgroud_width,contentWidth：",_display.background.width,contentWidth);
			_display.background.height = contentHeight + 18;
		}
		protected function get background():DisplayObject
		{
			return _display.background;
		}
		protected function get contentWidth():int
		{
			return _display.content.width;
		}
		protected function get contentHeight():int
		{
			return _display.content.height;
		}
		
		static protected var tip:ToolTip;
		static protected var dic:Dictionary;
		static public function bind(display:EventDispatcher,content:Object,over:String=MouseEvent.ROLL_OVER,out:String=MouseEvent.ROLL_OUT,click:String=MouseEvent.CLICK):void
		{
			if(!tip)
			{
				tip = new ToolTip();
				dic = new Dictionary(true);
			}
			if(!display)
			{
				return;
			}
			if(dic[display])
			{
				unbind(display);
			}
			var obj:Object = {};
			obj.content = content;
			obj.over = over;
			obj.out = out;
			obj.click = click;
			dic[display] = obj;
			
			display.addEventListener(obj.over,handler);
			display.addEventListener(obj.out,handler);
			display.addEventListener(obj.click,handler);
		}
		static public function unbind(display:EventDispatcher):void
		{
			if(!tip)
			{
				tip = new ToolTip();
				dic = new Dictionary(true);
			}
			
			var obj:Object = dic[display];
			if(!obj)
			{
				return;
			}
			display.removeEventListener(obj.over,handler);
			display.removeEventListener(obj.out,handler);
			delete dic[display];
		}
		static protected function handler(event:Event):void
		{
			var obj:Object = dic[event.currentTarget];
			if(!obj)
			{
				return;
			}
			if(event.type == obj.over)
			{
				tip.content = (obj.content is Function)?(obj.content()):(obj.content);
				tip.show();
			}else
			{
				tip.hide();
			}
		}
		
	}
}